﻿@{
    ViewBag.Title = "调拨管理";
    Layout = "~/Views/Shared/_LayoutPage.cshtml";
}
@section Contents{
    <style>
        .layui-form-label {
            width: auto;
        }
    </style>
}

<div id="page"></div>

<script type="text/template" id="tplPage">
    <div class="layui-fluid">
        <div class="layui-card">
            <div class="layui-form layui-card-header layuiadmin-card-header-auto">
                <div class="layui-form-item">
                    <div class="layui-inline">
                        <label class="layui-form-label" v-html="lang.Field.ToWarehouse">接收仓：</label>
                        <div class="layui-input-inline">
                            <select id="selWarehouse" lay-filter="selWarehouse">
                                <option value="">请选择</option>
                            </select>
                        </div>
                    </div>
                    <div class="layui-inline">
                        <label class="layui-form-label" v-html="lang.Field.WorkorderNo">工单号</label>
                        <div class="layui-input-inline">
                            <input type="text" id="txtWorkOrderNo" value="@ViewBag.WorkOrderNo" placeholder="请输入工单号" class="layui-input">
                        </div>
                    </div>
                    <div class="layui-inline">
                        <label class="layui-form-label" v-html="lang.Field.AllocateOrderNo">调拨单号</label>
                        <div class="layui-input-inline">
                            <input type="text" id="txtAllocateOrderNo" placeholder="请输入调拨单号" class="layui-input">
                        </div>
                    </div>
                    <div class="layui-inline">
                        <label class="layui-form-label" v-html="lang.Field.Date">日期：</label>
                        <div class="layui-input-inline">
                            <input type="text" id="txtRangeDate" value="@(DateTime.Today.AddDays(-7).ToString("yyyy-MM-dd")+" 至 "+DateTime.Today.ToString("yyyy-MM-dd"))" placeholder="请选择日期" class="layui-input">
                        </div>
                    </div>

                    <div class="layui-inline">
                        <button id="btnSearch" class="layui-btn layuiadmin-btn-list" type="button">
                            <i class="layui-icon layui-icon-search layuiadmin-button-btn"></i>
                        </button>
                        <button id="btnAdd" class="layui-btn layui-btn-normal layuiadmin-btn-list" type="button">
                            <i class="layui-icon layui-icon-addition layuiadmin-button-btn"></i>
                        </button>
                    </div>
                </div>
            </div>
        </div>

        <div class="layui-card">
            <div class="layui-card-body">
                <table id="table" lay-filter="tableEvent"></table>
            </div>
        </div>
    </div>
</script>

@section Scripts{
    <script>
        var vm = new Vue({
            el: "#page",
            template: "#tplPage",
            data: {
                lang: JSON.parse(lang).package
            }
        });

        setTimeout(function () {
            initLayui();
            initWarehouse();
        }, 50);
        function initLayui() {
            layui.use(['table', 'layer', 'form', 'laydate'], function () {
                var table = layui.table,
                    laydate = layui.laydate;

                laydate.render({
                    elem: '#txtRangeDate'
                    , range: '至'
                });

                var options = {
                    elem: '#table'
                    , url: '/Plan/QueryAllocateOrders'
                    , method: "post"
                    , where: { toWarehouseCode: "", workOrderNo: "@ViewBag.WorkOrderNo", allocateNo: "", startDate: "@(DateTime.Today.AddDays(-7).ToString("yyyy-MM-dd"))", endDate: "@(DateTime.Today.ToString("yyyy-MM-dd"))" }
                    , page: {
                        limit: 15
                        , limits: [15, 20, 25, 30, 40, 50]
                    }
                    , response: {
                        statusName: 'ErrCode'
                        , statusCode: 200
                        , msgName: 'ErrCode'
                        , countName: 'TotalCount'
                        , dataName: 'Data'
                    }
                    , cols: [[
                        { type: 'numbers', title: '序号', width: 60, fixed: "left" }
                        , { field: 'AllocateNo', title: '调拨单号', minWidth: 150, fixed: "left" }
                        , { field: 'WorkOrderNo', title: '工单号', minWidth: 150 }
                        , { field: 'FromWarehouse', title: '发出仓库', minWidth: 150 }
                        , { field: 'ToWarehouse', title: '接收仓库', minWidth: 150 }
                        , { field: 'AllocateTimeStr', title: '调拨时间', minWidth: 170 }
                        //, {
                        //    title: 'ERP同步', width: 100, templet: function (d) {
                        //        return "<span style='color:" + (d.BatchAsyncFinished ? "#009688" : "#C6C6C6") + ";'>" + (d.BatchAsyncFinished ? "已完成" : "未完成") + "</span>";
                        //    }
                        //}
                        //, { field: 'BatchLastAsyncTimeStr', title: '同步时间', minWidth: 150 }
                        , {
                            title: '操作', minWidth: 220, fixed: "right", templet: function (d) {
                                var htmlStr = '';
                                if (d.ReviewBy == 0) {
                                    htmlStr += `<button class="layui-btn layui-btn-sm layui-btn-warm" lay-event="review">审核</button>
                                                <button class="layui-btn layui-btn-sm layui-btn-normal" lay-event="edit">编辑</button>`;
                                }

                                htmlStr += '<button class="layui-btn layui-btn-sm layui-btn-warn" lay-event="detail">查看详情</button>';
                                return htmlStr;
                            }
                        }
                    ]]
                };

                $("#btnSearch").click(function () {
                    queryTable();
                });
                queryTable();
                function queryTable() {
                    options.page.curr = 1;
                    options.where.toWarehouseCode = $("#selWarehouse").val() == "" ? "" : $("#selWarehouse").val();
                    options.where.workOrderNo = $("#txtWorkOrderNo").val();
                    options.where.allocateNo = $("#txtAllocateOrderNo").val();
                    var rangeTime = $("#txtRangeDate").val().trim() == '' ? new Array() : $("#txtRangeDate").val().trim().split(' 至 ');
                    options.where.startDate = rangeTime.length <= 0 ? null : rangeTime[0];
                    options.where.endDate = rangeTime.length <= 0 ? null : rangeTime[1];
                    if (table.index <= 0) {
                        table.render(options);
                    } else {
                        table.reload("table", options);
                    }
                }

                table.on('tool(tableEvent)', function (obj) {
                    var data = obj.data;
                    var event = obj.event;

                    if (event == 'review') {
                        layer.confirm('确定审核该调拨单？', { icon: 3, title: '提示' }, function () {
                            $.ajax({
                                url: '/Plan/ReviewAllocateOrder',
                                data: {
                                    allocateNo: data.AllocateNo
                                },
                                dataType: 'json',
                                success: function (res) {
                                    if (res.ErrCode != 200) {
                                        return layer.msg(res.ErrMsg, { icon: 5 });
                                    }

                                    layer.msg('审核通过', { icon: 6 });
                                    queryTable();
                                }
                            });
                        });
                    } else if (event == 'edit') {
                        addAllocateOrder(data.AllocateNo);
                    } else if (event == 'detail') {
                        parent.parent.layui.index.openTabsPage("/Plan/AllocateOrderDetail?allocateNo=" + data.AllocateNo, "调拨单详情【" + data.AllocateNo + "】");
                    } else if (event == "asyncBatch") {
                        var loadId = layer.load();
                        $.ajax({
                            url: "/AllocateOrder/AsyncEsbAllocateOrders",
                            data: {
                                allocateNo: data.AllocateNo
                            },
                            complete: function () {
                                layer.close(loadId);
                            },
                            success: function (res) {
                                if (res.ErrCode != 200) {
                                    layer.msg(res.ErrMsg, { icon: 5 });
                                    return;
                                }
                                layer.msg("同步结束！", { icon: 6 });
                            }
                        });
                    }
                });

                $("#btnAdd").click(function () {
                    addAllocateOrder();
                });

                function addAllocateOrder(allocateNo) {
                    layer.open({
                        type: 2,
                        title: '编辑调拨单',
                        area: ['500px', '480px'],
                        content: '/Plan/EditAllocateOrder?allocateNo=' + allocateNo,
                        end: function () {
                            queryTable();
                        }
                    });
                }
            });
        }

        function initWarehouse() {
            $.ajax({
                url: '/Warehouse/GetAllWarehouse',
                type: 'post',
                dataType: 'json',
                success: function (res) {
                    console.log(res)
                    if (+ res.ErrCode === 200) {
                        appendToSelect(res.Data)
                    }
                }
            })
        }

        function appendToSelect(obj) {
            if (obj && obj.length) {
                for (let i = 0; i < obj.length; i++) {
                    let opt = obj[i];
                    let option = new Option(opt.WarehouseName, opt.WarehouseCode);
                    $('#selWarehouse').append(option);
                }
                layui.form.render('select');
            }
        }
    </script>
}
